
class_name ScenePointer
extends Node2D




## 移动范围模式
enum Range_Mode {
	# 默认模式，默认模式需要设置限制值
	NONE,
	# 地图模式，该模式将会限制 pointer 在 Map 之内
	# 该模式下将无视限制值
	MAP,
	# 混合模式
	# 该模式下要求即满足限制值又满足地图值
	MIX
}

# 指针模式
enum Pointer_Mode {
	
}

# 最大值
@export var max_position: Vector2 = Vector2(1920, 1920)
# 最小值
@export var min_position: Vector2 = Vector2(0, 0)
# 步长
@export var step: int = 64
# 移动模式
@export var range_mode: Range_Mode = Range_Mode.NONE
# 图标
@onready var icon: Sprite2D = $Icon

# 指针调用信号
signal pointer_callable_signal
# 指针移动
signal pointer_move_signal

# 设置是否启用
var is_enable: bool = true




# 设置启用状态
func set_enable(_enable: bool) -> void:
	is_enable = _enable
	if is_enable:
		icon.modulate = Color8(255, 255, 255)
	else:
		icon.modulate = Color(127, 127, 127)




func _input(_event: InputEvent) -> void:
	if not is_enable:
		return
	
	if _event.is_action_pressed("pointer_down", true):
		move_pointer(Vector2i.DOWN)
	
	elif _event.is_action_pressed("pointer_up", true):
		move_pointer(Vector2i.UP)
	
	elif _event.is_action_pressed("pointer_left", true):
		move_pointer(Vector2i.LEFT)
	
	elif _event.is_action_pressed("pointer_right", true):
		move_pointer(Vector2i.RIGHT)
	
	# 确认事件
	elif _event.is_action_pressed("action_confirm"):
		action_confirm()




# 处理移动，传入一个方向
func move_pointer(_direction: Vector2) -> void:
	# 如果没有方向就直接返回
	if _direction == Vector2.ZERO:
		return
	
	# 默认模式
	var rangmode_none_call: Callable = func (_value: Vector2) -> bool:
		# 判断位置是否在范围内
		if	_value.x <= max_position.x and _value.y <= max_position.y and \
			_value.x >= min_position.x and _value.y >= min_position.y:
			return true
		
		return false
	
	# 地图模式
	var rangmode_map_call: Callable = func (_value: Vector2i) -> bool:
		if get_parent().map.get_position_call_is_empty(_value):
			return true
		return false
	
	# 混合模式
	var rangmode_mix_call: Callable = func (_value: Vector2) -> bool:
		if rangmode_none_call.call(_value) and rangmode_map_call.call(_value):
			return true
		
		return false
	
	var next_position: Vector2 = (position as Vector2) + _direction * step
	
	if range_mode == Range_Mode.NONE:
		if not rangmode_none_call.call(next_position):
			return
	
	elif range_mode == Range_Mode.MAP:
		if not rangmode_map_call.call(next_position):
			return
	
	elif range_mode == Range_Mode.MIX:
		if not rangmode_mix_call.call(next_position):
			return
	
	position = next_position
	emit_signal("pointer_move_signal", self)




# 发出调用信号
func action_confirm() -> void:
	emit_signal("pointer_callable_signal", self)
